package pe.com.bbva.mantenimiento.bo.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import pe.com.bbva.mantenimiento.bo.KitBO;
import pe.com.bbva.mantenimiento.dao.KitDAO;
import pe.com.bbva.mantenimiento.domain.Kit;
import pe.com.bbva.mantenimiento.domain.Material;
import pe.com.bbva.util.Constantes;
import pe.com.stefanini.core.bo.GenericBOImpl;
import pe.com.stefanini.core.bo.Grid;
import pe.com.stefanini.core.exceptions.BOException;
import pe.com.stefanini.core.exceptions.DAOException;

@Service("kitBO")
@SuppressWarnings({"rawtypes","unchecked"})
public class KitBOImpl extends GenericBOImpl implements KitBO{
	
	@Resource
	private KitDAO kitDAO;
	
	private String codigoAnterior;
	
	public Kit findById(Long id) throws BOException {
		Kit kit = (Kit) super.findById(Kit.class, id);
		return kit;
	}

	public List<Kit> findKits(Kit kit)
			throws BOException, DAOException {
		List<Kit> listaKits = kitDAO.findKits(kit);
		return listaKits;
	}

	public boolean validate(Kit kit) throws BOException, DAOException{
		if(kit.getId()==null && !kit.getApMasiva().equals(Constantes.ESTADO_ACTIVO)){
			Kit kitBuscar = new Kit();
			kitBuscar.setTipoTarjeta(kit.getTipoTarjeta());
			List<Kit> kits = findKits(kitBuscar);
			if(kits!=null && !kits.isEmpty()){
				throw new BOException("El kit ya existe");
			}
		}
//		if(!codigoAnterior.equals(kit.getCodGPS())&& !kit.getApMasiva().equals(Constantes.ESTADO_ACTIVO)){
//			Kit kitBuscar = new Kit();
//			kitBuscar.setCodGPS(kit.getCodGPS());
//			List<Kit> kits = findKits(kitBuscar);
//			if(kits!=null && !kits.isEmpty()){
//				throw new BOException("El c\u00F3digo GPS ya esta registrado");	
//			}
//		}
		if(kit.getApMasiva().equals(Constantes.ESTADO_ACTIVO)){
			Kit kitBuscar = new Kit();
			kitBuscar.setApMasiva(kit.getApMasiva());
			List<Kit> kits = findKits(kitBuscar);
			if(kits!=null && !kits.isEmpty()){
				throw new BOException("El kit para Aperturas Masivas ya existe");
			}
		}
		return true;
	}
	
	public Grid<Material> findToGrid(List<Material> materiales,String order, int page, int rows) throws BOException, DAOException{
		String where="where ";	
	    return super.findToGridList(materiales, where, " order by "+order,page,rows);
	}
	
	@Transactional(propagation=Propagation.REQUIRED)
	public void save(Kit kit, String codigoAnterior) throws BOException, DAOException {
		this.codigoAnterior = codigoAnterior;
		if(validate(kit))
			super.doSave(kit);
	}

	@Transactional(propagation=Propagation.REQUIRED)
	public void delete(Kit kit) throws BOException {
		super.delete(kit);
	}

	@Override
	public Date maxFechaMaterial(Long idTipoMaterial, Long idKit)
			throws BOException, DAOException {
		return kitDAO.maxFechaMaterial(idTipoMaterial,idKit);
	}
}